package com.jinxin.platform.cdacsrecord.core.task;

import ch.qos.logback.classic.gaffer.PropertyUtil;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.DataSyncFrequency;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.CommonCodeEntity;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.Constant;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.ResultEntity;
import com.jinxin.platform.cdacsrecord.core.redis.RedisUtil;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-09-18
 * @Author: Lingnan
 **/
@Component
public class SchedulerAllJob {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    @Value("${cdacsrecord.openRecordTimerCron}")
    private String openRecordTimerCron;
    @Value("${cdacsrecord.capRecordTimerCron}")
    private String capRecordTimerCron;
    @Autowired
    private RedisUtil redisUtil;

    /*
     * 调用定时任务
     */
    public void scheduleDayJobs() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        scheduleDayJob(scheduler);
    }
    /**
     * 配置定时任务
     * @param scheduler
     * @throws SchedulerException
     */
    private void scheduleDayJob(Scheduler scheduler) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class).withIdentity("perhour", "group1").build();
        JobDetail jobDetail2 = JobBuilder.newJob(ScheduledJob2.class).withIdentity("perhour2", "group2").build();
        //设置定时规则
        String key = Constant.DATA_SYNC_FREQUENCY;
        String timeCron1 = openRecordTimerCron;
        String timeCron2 = capRecordTimerCron;
        if(redisUtil.hasKey(key)){
            String jsonStr = (String)redisUtil.get(key);
            if(!StringUtils.isEmpty(jsonStr)){
                DataSyncFrequency dataSyncFrequency = JSONObject.parseObject(jsonStr, DataSyncFrequency.class);
                int openSyncFre = dataSyncFrequency.getOpenSyncFre();
                int capSyncFre = dataSyncFrequency.getCapSyncFre();
                if(openSyncFre > 0){
                    timeCron1 = "0 0/"+openSyncFre+" * * * ? ";
                }
                if(capSyncFre > 0){
                    timeCron2 = "0 0/"+capSyncFre+" * * * ? ";
                }
            }
        }
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(timeCron1);
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
                .withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);

        CronScheduleBuilder scheduleBuilder2 = CronScheduleBuilder.cronSchedule(timeCron2);
        CronTrigger cronTrigger2 = TriggerBuilder.newTrigger().withIdentity("trigger2", "group2")
                .withSchedule(scheduleBuilder2).build();
        scheduler.scheduleJob(jobDetail2, cronTrigger2);
    }

}
